package com.px.user.controller;

import com.px.common.core.Response;
import com.px.user.entity.AdminOps;
import com.px.user.entity.ChargeChannel;
import com.px.user.mapper.ChargeChannelMapper;
import com.px.user.model.ActionModel;
import com.px.user.model.AdminOpsCategory;
import com.px.user.model.PagingResp;
import com.px.user.service.AdminOpsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/chargeChannel")
@Api(value = "支付通道", tags = {"支付通道管理"})
public class ChargeChannelController {

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private ChargeChannelMapper chargeChannelMapper;

    @Autowired
    private AdminOpsService adminOpsService;

    @PostMapping("/create")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation(value = "添加支付渠道", authorizations = {@Authorization(value = "token")})
    public Response chargeChannelPost(
            @Validated(ActionModel.Insert.class) @RequestBody ChargeChannel chargeChannel,
            Authentication authentication) {
        chargeChannelMapper.insert(chargeChannel);

        AdminOps adminOps = AdminOps.builder()
                .category(AdminOpsCategory.CREATE_PAY.getStatus())
                .remark(AdminOpsCategory.CREATE_PAY.getMsg())
                .url(httpServletRequest.getRequestURI())
                .value(chargeChannel.getId().toString())
                .build();
        adminOpsService.setRole(adminOps, authentication);
        adminOpsService.insert(adminOps);

        return Response.success(chargeChannel.getId());
    }

    @PutMapping("/update")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation(value = "更新支付通道", authorizations = {@Authorization(value = "token")})
    public Response chargeChannelPut(
            @Validated(ActionModel.Update.class) @RequestBody ChargeChannel chargeChannel,
            Authentication authentication) {
        chargeChannelMapper.update(chargeChannel);

        AdminOps adminOps = AdminOps.builder()
                .category(AdminOpsCategory.UPDATE_PAY.getStatus())
                .remark(AdminOpsCategory.UPDATE_PAY.getMsg())
                .url(httpServletRequest.getRequestURI())
                .value(chargeChannel.getId().toString())
                .build();
        adminOpsService.setRole(adminOps, authentication);
        adminOpsService.insert(adminOps);

        return Response.success(null);
    }

    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('ADMIN')")
    @ApiOperation(value = "删除支付通道", authorizations = {@Authorization(value = "token")})
    public Response chargeChannelDelete(@ApiParam(value = "通道ID", example = "4") @PathVariable("id") Long id,
                                        Authentication authentication) {
        chargeChannelMapper.delete(id);

        AdminOps adminOps = AdminOps.builder()
                .category(AdminOpsCategory.UPDATE_PAY.getStatus())
                .remark(AdminOpsCategory.UPDATE_PAY.getMsg())
                .url(httpServletRequest.getRequestURI())
                .value(id.toString())
                .build();
        adminOpsService.setRole(adminOps, authentication);
        adminOpsService.insert(adminOps);

        return Response.success(null);
    }

    @GetMapping("/search")
    @ApiOperation(value = "支付通道列表", authorizations = {@Authorization(value = "token")})
    public Response<PagingResp<ChargeChannel>> chargeChannelGet(
            @ApiParam(value = "id") @RequestParam(value = "id", required = false) Long id,
            @ApiParam(value = "通道名称") @RequestParam(value = "name", required = false) String name,
            @ApiParam(value = "通道地址") @RequestParam(value = "url", required = false) String url,
            @ApiParam(value = "可选金额列表") @RequestParam(value = "value", required = false) String value,
            @ApiParam(value = "最小金额") @RequestParam(value = "start", required = false) BigDecimal start,
            @ApiParam(value = "最大金额") @RequestParam(value = "end", required = false) BigDecimal end,
            @ApiParam(value = "已禁用: 1#禁用,0#未禁用") @RequestParam(value = "disabled", required = false) Integer disabled,
            @ApiParam(value = "开始时间", example = "1566805792293") @RequestParam(value = "qstart", required = false) Long qstart,
            @ApiParam(value = "截止时间") @RequestParam(value = "qend", required = false) Long qend,
            @ApiParam(value = "排序字段(倒序)", example = "createTime") @RequestParam(value = "orderField", required = false, defaultValue = "createTime") String orderField,
            @ApiParam(value = "页码", example = "0") @RequestParam(value = "page", required = false, defaultValue = "0") @Min(0) Integer page,
            @ApiParam(value = "返回数量", example = "10") @RequestParam(value = "limit", required = false, defaultValue = "10") @Min(1) Integer limit) {

        ChargeChannel qchargeChannel = ChargeChannel.builder()
                .id(id)
                .name(name)
                .url(url)
                .value(value)
                .start(start)
                .end(end)
                .disabled(disabled)
                .qstart(qstart == null ? null : new Timestamp(qstart))
                .qend(qend == null ? null : new Timestamp(qend))
                .orderField(orderField)
                .startIndex(page * limit)
                .limit(limit)
                .build();

        List<ChargeChannel> chargeChannels = chargeChannelMapper.paging(qchargeChannel);
        return Response.success(PagingResp.<ChargeChannel>builder()
                .itemCount(chargeChannelMapper.pagingCount(qchargeChannel))
                .items(chargeChannels).build());
    }

}
